package com.bdqn.wang.servlet;

import com.alibaba.fastjson.JSON;
import com.bdqn.wang.pojo.News;
import com.bdqn.wang.service.NewsService;
import com.bdqn.wang.service.impl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/news"})
public class NewsSevlet extends HttpServlet {
    NewsService newsService = new NewsServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String opr = req.getParameter("opr");
        if ("list".equals(opr)){
            req.getRequestDispatcher("/WEB-INF/NewsMore.jsp").forward(req,resp);
        }else if("listAll".equals(opr)){
            List<News> more = newsService.getAll();
            req.getSession().setAttribute("listAll",more);
//            String json = JSONArray.toJSONString(orderList);
            resp.getWriter().print(JSON.toJSONString(more));
        }
        }
    }

